package com.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;
import com.gulimall.common.vo.SkuEsModel;
import com.gulimall.product.dao.SpuInfoDao;
import com.gulimall.product.entity.SkuInfoEntity;
import com.gulimall.product.entity.SpuInfoEntity;
import com.gulimall.product.service.SkuInfoService;
import com.gulimall.product.service.SpuInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {


    @Autowired
    private SkuInfoService skuInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void up(long spuId) {

        //1. 构建EsModels   spuId--> skus --> SkuEsModels

        List<SkuInfoEntity> skusBySpuId = skuInfoService.getSkusBySpuId(spuId);
        List<SkuEsModel> skuEsModels = skusBySpuId.stream().map((sku) -> {
            SkuEsModel skuEsModel = new SkuEsModel();

            BeanUtils.copyProperties(sku,skuEsModel);

            //剩余属性
            //private boolean hasStock = true; //是否有库存   库存微服务里找
            // select  sku_id,sum(ifnull(stock,0)-ifnull(stock_locked,0))
            //from
            //wms_ware_sku
            //where sku_id = 2

            // select xx from w  where attrid in <foreach></foreach>


            //private List<Attr> attrs; //sku对应的所有属性集合  查询和spu相关的并且需要查询的属性
            // spuId -->  product-attr[ attrids ]  -->  在attr表中searchType为1


            return skuEsModel;

        }).collect(Collectors.toList());


        //调用search微服务  添加skuEsModel   bulk


    }

}

// 上架代码
// 2. 话术